package com.atlin.project.service.impl;

import com.atlin.project.contant.CommonConstant;
import com.atlin.project.mapper.className_CMapper;
import com.atlin.project.model.domain.className_C;
import com.atlin.project.model.dto.className_L.className_CQueryRequest;
import com.atlin.project.model.vo.className_CVO;
import com.atlin.project.service.className_CService;
import com.atlin.project.utils.sql.SqlUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author atlin
 * @description className_L
 * @createDate date
 */
@Service
public class className_CServiceImpl extends ServiceImpl<className_CMapper, className_C>
        implements className_CService {

    @Override
    public void valid(className_C className_L, boolean add) {

    }

    @Override
    public className_CVO getclassName_CVO(className_C className_L, HttpServletRequest request) {
        className_CVO className_LVO = className_CVO.objToVo(className_L);
        // todo other operation
        return className_LVO;
    }

    /**
     * 获取查询包装类（用户根据哪些字段查询，根据前端传来的请求对象，得到 mybatis 框架支持的查询 QueryWrapper 类）
     *
     * @param className_LQueryRequest
     * @return
     */
    @Override
    public QueryWrapper<className_C> getQueryWrapper(className_CQueryRequest className_LQueryRequest) {
        QueryWrapper<className_C> queryWrapper = new QueryWrapper<>();
        if (className_LQueryRequest == null) {
            return queryWrapper;
        }
        Long id = className_LQueryRequest.getId();
        String title = className_LQueryRequest.getTitle();
        String content = className_LQueryRequest.getContent();
        String sortField = className_LQueryRequest.getSortField();
        String sortOrder = className_LQueryRequest.getSortOrder();

        queryWrapper.like(StringUtils.isNotBlank(title), "title", title);
        queryWrapper.like(StringUtils.isNotBlank(content), "content", content);
        queryWrapper.eq(ObjectUtils.isNotEmpty(id), "id", id);
        queryWrapper.eq("isDelete", false);
        queryWrapper.orderBy(SqlUtils.validSortField(sortField),
                sortOrder.equals(CommonConstant.SORT_ORDER_ASC), sortField);

        // todo other operation
        return queryWrapper;
    }

    @Override
    public Page<className_CVO> getclassName_CVOPage(Page<className_C> className_LPage, HttpServletRequest request) {
        List<className_C> className_LList = className_LPage.getRecords();
        Page<className_CVO> className_LVOPage = new Page<>(className_LPage.getCurrent(), className_LPage.getSize(), className_LPage.getTotal());
        if (CollectionUtils.isEmpty(className_LList)) {
            return className_LVOPage;
        }
        // todo other operation

        // 填充信息
        List<className_CVO> className_LVOList = className_LList.stream().map(className_L -> {
            className_CVO className_LVO = className_CVO.objToVo(className_L);
            // todo other operation

            return className_LVO;
        }).collect(Collectors.toList());
        className_LVOPage.setRecords(className_LVOList);
        return className_LVOPage;
    }
}




